/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vectors.Patterned;

import Vectors.IVector;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author netcracker
 */
public class JVectorAdapter implements IVector {

    private Vector<Double> v;
    private boolean edited = false;
    private ArrayList<Observer> observers;

    public JVectorAdapter(Vector vector) {
        v = vector;
    }

    @Override
    public void add(double o) {
        v.add(o);
    }

    @Override
    public void delete(int index) {
        v.remove(index);
    }

    @Override
    public Double get(int id) {
        return v.elementAt(id);
    }

    @Override
    public void set(double value, int position) {
        v.set(position, value);
    }

    @Override
    public void fill(double[] mass) {
        if (this.getLength() == mass.length) {
            v.clear();
            for (int i = 0; i < this.getLength(); i++) {
                v.add(mass[i]);
            }
        }
    }

    @Override
    public void fill(IVector myVector) {
        if (this.getLength() == myVector.getLength()) {
            v.clear();
            for (int i = 0; i < this.getLength(); i++) {
                v.add(myVector.get(i));
            }
        }
    }

    @Override
    public boolean isSame(IVector myVector) {
        if (this.v.size() == myVector.getLength()) {
            for (int i = 0; i < this.v.size(); i++) {
                boolean b = this.v.get(i) == myVector.get(i);
                if (b == false) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int getLength() {
        return v.size();
    }

    @Override
    public void multiply(double f) {
        for (int i = 0; i < this.v.size(); i++) {
            this.v.set(i, this.v.get(i) * f);
        }
    }

    @Override
    public void plus(IVector myVector) {
        if (this.v.size() == myVector.getLength()) {

            for (int i = 0; i < this.v.size(); i++) {
                this.v.set(i, this.v.get(i) + myVector.get(i));
            }
        }
    }

    @Override
    public void sort() {
        for (int i = this.v.size() - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (this.v.get(j) > this.v.get(j + 1)) {
                    this.swap(j, j + 1);
                }
            }
        }
    }

    @Override
    public void swap(int i, int j) {
        Double t = this.v.get(i);
        this.v.set(i, this.v.get(j));
        this.v.set(j, t);
    }

    @Override
    public Iterator<Double> iterator() {
        return new Iterator<Double>() {
            int marker = 0;

            @Override
            public boolean hasNext() {
                if (v.size() > marker) {
                    return true;
                }
                return false;
            }

            @Override
            public Double next() {
                double result = v.get(marker);
                marker++;

                return result;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };
    }

    @Override
    public boolean equals(Object obj) {
        //obj = (IVector) obj;
        if (obj == null) {
            return false;
        }

        if (!(obj instanceof IVector)) {
            return false;
        }
        final IVector other = (IVector) obj;

        if (this.getLength() != other.getLength()) {
            return false;
        }
        if (this.hashCode() != obj.hashCode()) {
            return false;
        }

        if (!(this.isSame(other))) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        long result = Double.doubleToLongBits(this.get(0));
        int ups;
        int downs;

        for (int i = 1; i < this.v.size() - 1; i++) {
            result = result ^ Double.doubleToLongBits(this.v.get(i));
        }
        downs = (int) result;
        result = result >> 32;
        ups = (int) result;
        return downs ^ ups;
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer(this.getClass().toString());
        stringBuffer.append("\n" + "Contains:" + "\n");
        for (int i = 0; i < this.v.size(); i++) {
            stringBuffer.append(this.v.get(i)).append("\n");
        }
        return stringBuffer.toString();
    }

    @Override
    public JVectorAdapter clone() throws CloneNotSupportedException {
        JVectorAdapter jva = (JVectorAdapter) super.clone();
        jva.v = (Vector<Double>) this.v.clone();
        return jva;
    }

    @Override
    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyToObservers() {
        for (Observer observer : observers) {
            observer.updated();
        }
    }

    @Override
    public void notifyToObservers(int index, double value) {
        for (Observer observer : observers) {
            observer.updated(index, value);
        }
    }
}
